<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/16 0016
 * Time: 15:21
 */
namespace Home\Controller;

use Think\Controller;

class CustomerController extends Controller
{
    /***
     *前端用户AJAX 注册
     */
    public function register(){
        $Customer = M('Customer');
        if(IS_AJAX){
            $data['username'] = trim(I('post.username'));
            $data['password'] = set_password(I('post.password'));
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $data['sex'] = I('post.sex');
            $data['birthday'] = I('post.birthday');
            $check_password = I('post.check_password');
            //验证邮箱
            $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
            //验证手机号
            $telphone = "#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#";
            //验证唯一

            $info = array('status' =>1, 'msg' =>'注册成功');
            $username = $Customer->where("username='$data[username]'")->find();
            $email = $Customer->where("email='$data[email]'")->find();
            $mobile = $Customer->where("mobile='$data[mobile]'")->find();
            if($username){
                $info = array('status' =>0, 'msg' =>'用户名已被注册');
            }
            else if($email){
                $info = array('status' =>0, 'msg' =>'邮箱已被注册');
            }
            else if($mobile){
                $info = array('status' =>0, 'msg' =>'手机号已被注册');
            }
            else if(strlen($data['username'])<6){
                $info = array('status' =>0, 'msg' =>'用户名长度不能少于6位');
            }
            else if (!preg_match( $pattern, $data['email'] ) ){
                $info = array('status' =>0, 'msg' =>'请填写正确邮箱格式');
            }
            else if (!preg_match( $telphone, $data['mobile'] ) ){
                $info = array('status' =>0, 'msg' =>'请填写正确手机号');
            }
            else if(strlen(I('post.password'))<6){
                $info = array('status' =>0, 'msg' =>'密码长度不能少于6位');
            }
            else if($check_password!=I('post.password')){
                $info = array('status' =>0, 'msg' =>'两次密码填写不一致');
            }
            if($info['status']==1){
                $Customer->create();
                $Customer->add($data);
            }
            $this->ajaxReturn($info);
        }else{
            $this->display();
        }
    }
    /***
     * 前端用户ajax 登录
     */
    public function login(){
        $Customer = M('Customer');
        if(IS_AJAX){
//            dump($_POST);exit;
            $map['username|email|mobile'] = trim(I('post.username'));
            $map['password'] = set_password(I('post.password'));
//            $code = I('post.code');

            $customer =  $Customer->where($map)->find();


            if(!$customer){
                $info = array('status'=>0,'msg'=>"用户名或密码错误");
//            }else if(!$this->check_verify($code)){
//                $info = array('status'=>0,'msg'=>"验证码输入错误");
            }else {
                $_SESSION['customer'] = $customer;
                $info = array('status'=>1,'msg'=>"登录成功");
            }
            $this->ajaxReturn($info);
        }else{
            $this->display();
        }
    }

}